package c2.advanced.business.common.fight;

import cate.common.util.GameResult;
import cate.game.fight.FightTaskContext;
import cate.game.play.config.AllPlayCfg;
import cate.game.play.part.FightSide;
import cate.game.play.proce.play.PlayCtx;
import cate.game.play.proce.play.PlayPO;
import cate.game.play.support.PlayBuildContext;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class InternalFightHandler {
    public static InternalFightHandler INSTANCE = new InternalFightHandler();

    private InternalFightHandler() {

    }

    final AllPlayCfg cfg = new AllPlayCfg();

    public void setUp(PlayBuildContext context) {
        cfg.init(context);
    }

    public GameResult<PlayPO> start(FightTaskContext ftc, FightSide a, FightSide b) {
        GameResult<PlayPO> r = new GameResult<>();
        try {
            PlayCtx ctx = new PlayCtx();
            long t1 = System.currentTimeMillis();
            ctx.exe(cfg, ftc, a, b);
            long t2 = System.currentTimeMillis();
            r.data = ctx.po;
            if (t2 - t1 >= 1000) {
                log.info("战斗计算耗时较长 时间：{}ms 功能id={},回合数={}", t2 - t1, ftc.funcId, ctx.po.rounds.size());
            }
            return r;
        } catch (Exception e) {
            log.error("PlayManager 发生error", e);
            return r.fail("战斗过程异常");
        }
    }
}